import java.util.concurrent.ArrayBlockingQueue;

public class ABQDemo {
    //构建大小为10的阻塞队列
    private static ArrayBlockingQueue<Integer> arrayBlockingQueue = new ArrayBlockingQueue<>(10);

    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            for (int i = 0; i <= 10; i++) {
                arrayBlockingQueue.add(i);
            }
        });
        thread1.start();
        try {
            //等待线程1
            thread1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
            if (!arrayBlockingQueue.offer(11)) {
                System.out.println("插入元素11失败!");
            }

            try {
                Thread.sleep(2000);
                arrayBlockingQueue.put(11);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        Thread thread2 = new Thread(()->{
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Integer e;
            System.out.println("开始出队:");
            while ((e = arrayBlockingQueue.poll())!=null){
                System.out.println("\t"+e);
            }
        });
        thread2.start();
    }
}
